package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting setting) {
        LambdaQueryWrapper<OrderSetting> wrapper = new LambdaQueryWrapper();
        wrapper.eq(OrderSetting::getOrderDate,setting.getOrderDate());
        if (orderSettingService.getOne(wrapper)==null){
            orderSettingService.add(setting);
        }else {
            orderSettingService.updatenumber(setting);}
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam Integer year, @RequestParam Integer month) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startDate= df.format(System.currentTimeMillis());
        String endDate=year+"-"+month+"-"+31;
        LambdaQueryWrapper<OrderSetting> wrapper = new LambdaQueryWrapper();
        wrapper.between(OrderSetting::getOrderDate,startDate,endDate);
        List<OrderSetting> list = orderSettingService.list(wrapper);
        List<Map<String, Object>> voList = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(orderSetting.getOrderDate());
            Map<String, Object> map = new HashMap<>();
            map.put("date", cal.get(Calendar.DAY_OF_MONTH));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            voList.add(map);
        }
       return new Result(true, MessageConst.ACTION_SUCCESS,voList);
    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile")  MultipartFile multipartFile) {
        String originalFileName = multipartFile.getOriginalFilename();
        Workbook workbook = null;
        try {
            InputStream fis = multipartFile.getInputStream();
            if(originalFileName.endsWith(".xls")){
                workbook = new HSSFWorkbook(fis);
            }else if(originalFileName.endsWith(".xlsx")){
                // excel 2007
                workbook = new XSSFWorkbook(fis);
            }else{
                return new Result(false,"文件格式不正确，请检查重试");
            }
            Sheet sheet = workbook.getSheetAt(0);
            Row row = null;
            OrderSetting setting=new OrderSetting();
            for(int i = 1;i<sheet.getPhysicalNumberOfRows();i++) {
                row = sheet.getRow(i);
                setting.setOrderDate(row.getCell(0).getDateCellValue());
                setting.setNumber((int) row.getCell(1).getNumericCellValue());
                LambdaQueryWrapper<OrderSetting> wrapper = new LambdaQueryWrapper();
                wrapper.eq(OrderSetting::getOrderDate,setting.getOrderDate());
                if (orderSettingService.getOne(wrapper)==null){
                    orderSettingService.add(setting);
                }else {
                    orderSettingService.updatenumber(setting);}
            }
                } catch (Exception e) {
                 e.printStackTrace();
                }
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

}
